package m2;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();
        c.queueDeclare("task-queue", true, false, false, null);
        DeliverCallback callback = new DeliverCallback() {

            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s = new String(message.getBody());
                System.out.println("收到："+s);
                for (int i = 0; i < s.length(); i++) {//s.length().fori
                    if('.'==s.charAt(i)){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //参数： 1，回执数据 2，是否同时确认以前收到的所有消息
                c.basicAck(message.getEnvelope().getDeliveryTag(),false);
                System.out.println("消息接收完成");


            }
        };
        CancelCallback cancel = new CancelCallback() {

            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };
        //qos=1，每次收一条，处理完之前不接收下一条
        //每次抓取的数量
        c.basicQos(1);
        c.basicConsume( "task-queue", false, callback, cancel);
    }
}
